# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import os


pathHtml='D:\\pypronjects\\tzzb-AppiumUI\\result\\html\\2018-11-21 21-28-38---result.html'
msg = MIMEMultipart('alternative')
msgText=MIMEText('测试','plain','utf-8')
msg.attach(msgText)

fp=open(pathHtml,'rb')
mail_body=fp.read()
fp.close()
msgHtml=MIMEText(mail_body,'html','utf-8')
msgHtml['Connect-type']='application/octet-stream'
msgHtml["Content-Disposition"] = 'attachment; filename="自动化测试报告.html"'
msg.attach(msgHtml)

pathImg='D:\\pypronjects\\tzzb-AppiumUI\\result\\screenshot\\2018-11-24\\shouye-Login(15619205143).png'
fp1=open(pathImg,'rb')
mail1_body=fp1.read()
name=os.path.basename(pathImg)
fp1.close()
msgImg=MIMEText(mail1_body,'base64','utf-8')
msgImg['Connect-type']='application/octet-stream'
msgImg["Content-Disposition"] = 'attachment; filename=%s',name
msg.attach(msgImg)




# 输入发送方邮箱地址和口令
from_addr ='185365234@qq.com'
passwd ='nbtrxpitvofxbjha' # QQ邮箱需要输入授权码，不含空格

# 输入收件人地址
to_addr ='sunyanfen@myhexin.com'

#编辑邮件的头部信息
msg['From'] = Header('孙毛线<%s>' %from_addr,'utf-8')
msg['To'] = Header('孙燕芬<%s>' %to_addr,'utf-8')
msg['Subject'] = Header('投资账本自动化运行结果','utf-8').encode()

        # 输入smtp服务器地址
smtp_server ='smtp.qq.com'

try:
    server = smtplib.SMTP_SSL(smtp_server, 465)  # 注意：需要建立ssl安全连接，更安全；465是QQ邮箱的smtp端口
    # server.set_debuglevel(1)  # 打印smtp服务器的交互信息
    server.login(from_addr, passwd)
    server.sendmail(from_addr=from_addr, to_addrs=to_addr,msg=msg.as_string())
    server.quit()
    print("邮件发送成功")
except smtplib.SMTPException, e:
    print e




